10. CODE: Write the Distance Function
Write the Distance Function
As you write A* search, it will be useful to be able to find the distance between two nodes. This will allow the search algorithm to find the closest node to the current node. In this exercise, you will write a utility function that finds the distance between two RouteModel::Node
objects.
## To complete this exercise:
- Add a
distance
declaration to theRouteModel::Node
class inroute_model.h
. This method should take aNode
object as the argument, and it should return afloat
. Thedistance
method shouldn't change the object being passed, so you can make it aconst
method (add const after the function name). - Return the euclidean distance from the current node to the node passed in. Note that for points (x_1, y_1) and (x_2, y_2), the euclidean distance is given by \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}.
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity, so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: react
- Opened files (when workspace is loaded): n/a
-
userCode:
export CXX=g++-7
export CXXFLAGS=-std=c++17
cmake_tests() {
/usr/local/bin/cmake -DTESTING="NodeDist" "$1"
}
export -f cmake_tests
Solution
Write The Distance Function